package com.testing.thread;


/**
 * @author lzp
 * 多线程交替输出1 2 3 4 5 6
 */
public class TestThreadSu {

    private static int i = 1;
    private static int j = 10;
    private static int statue = 0;

    public static void main(String[] args) {
        final TestThreadSu testThread = new TestThreadSu();
        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (testThread) {
                    for(;i < j;){
                        // 指定类型等待
                        while(statue!=0){
                            try {
                                testThread.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }

                        System.out.println(Thread.currentThread().getName()+":"+i);
                        i ++;
                        statue = 1;
                        testThread.notifyAll();
                    }
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (testThread) {
                    for(;i < j;){
                        while(statue!=1){
                            // 进来就让等待
                            try {
                                testThread.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }

                        System.out.println(Thread.currentThread().getName()+":"+i);
                        i ++;
                        statue = 0;
                        testThread.notifyAll();
                    }
                }
            }
        }).start();

    }
}
